// pages/business/business.js
const app = getApp()
const api = require('../../../utils/util')
Page({
    // 页面分享
    onShareAppMessage () {},
    // 分享朋友圈
    onShareTimeline () {},

    /**
     * 页面的初始数据
     */
    data: {
        imagesUrl: app.globalData.imagesUrl,
        siteUrl: app.globalData.siteUrl,
        // 切换置顶
        
        tabsList: [
            {
                title: '需求'
            },
            {
                title: '服务'
            },
            {
                title: '匹配'
            },
        ],
        tabsCurrent: 0,
        stickyScroll: false,
        showView: true,
        result: [],
        checked: false,
        supply1:[],
        // 
        result1: [],
        checked1: false,
        supply2:[],
        // 
        result2: ['a'],
        checked2: false,
        supply3:[],
        page_1: 1,
        page_2: 1,
        page_3: 1,
        is_count: false

      },

    /**
     * 生命周期函数--监听页面加载
     */
      // 全选
      checkboxChange(e) {
        this.setData({
          result: e.detail,
        });
        let b = []
        for(let i = 0; i<this.data.supply1.length; i++){
          b.push(this.data.supply1[i].id)
        }
        let c = e.detail.length === b.length && e.detail.filter(t => !b.includes(t));
        if (c) {
          this.setData({
            checked: true,
          });
        } else {
          this.setData({
            checked: false,
          });
        }
    
      },
      checkAll(e) {
        this.setData({
          checked: e.detail,
        });
        if (e.detail) {
          let b = []
          for(let i = 0; i<this.data.supply1.length; i++){
            b.push(this.data.supply1[i].id)
          }
          this.setData({
            result: b,
          });
        } else {
          this.setData({
            result: [],
          });
        }
      },
         // 全选2
         checkboxChange1(e) {
          this.setData({
            result1: e.detail,
          });
          let b = []
            for(let i = 0; i<this.data.supply2.length; i++){
              b.push(this.data.supply2[i].id)
            }
          let c = e.detail.length === b.length && e.detail.filter(t => !b.includes(t));
          if (c) {
            this.setData({
              checked1: true,
            });
          } else {
            this.setData({
              checked1: false,
            });
          }
      
        },
        checkAll1(e) {
          this.setData({
            checked1: e.detail,
          });
          if (e.detail) {
            let b = []
            for(let i = 0; i<this.data.supply2.length; i++){
              b.push(this.data.supply2[i].id)
            }
            this.setData({
              result1: b,
            });
          } else {
            this.setData({
              result1: [],
            });
          }
        },
           // 全选3
      checkboxChange2(e) {
        this.setData({
          result2: e.detail,
        });
        let b = []
            for(let i = 0; i<this.data.supply3.length; i++){
              b.push(this.data.supply3[i].id)
            }
        let c = e.detail.length === b.length && e.detail.filter(t => !b.includes(t));
        if (c) {
          this.setData({
            checked2: true,
          });
        } else {
          this.setData({
            checked2: false,
          });
        }
    
      },
      checkAll2(e) {
        this.setData({
          checked2: e.detail,
        });
        if (e.detail) {
          let b = []
          for(let i = 0; i<this.data.supply3.length; i++){
            b.push(this.data.supply3[i].id)
          }
          this.setData({
            result2: b,
          });
        } else {
          this.setData({
            result2: [],
          });
        }
      },
    
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
       let that = this
       this.setData({
         page_1:1,
         page_2:1,
         page_3:1
       })
       this.my_supply1(that, 1)
       this.my_supply2(that, 1)
       this.my_supply3(that, 1)
       this.memberSupply1_count(that)
    },



    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    // 点击管理
    onTabsClick(e) {
      let tabsCurrent = e.target.dataset.index
      this.setData({tabsCurrent})
    },
    onLoad: function (options) {
    // 生命周期函数--监听页面加载
    showView: (options.showView == "true" ? true : false)

    if(options.tabsCurrent){
      this.setData({
        tabsCurrent: options.tabsCurrent
      })
    }
    },
    
    onChangeShowState: function () {
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },


  my_supply1(that, page){
    let param = {
      ucode: app.globalData.userinfo.ucode
    }
    api.my_supply1(param, page).then(function(res){
      if (res.data.length>0){
        that.setData({
          supply1: page == 1 ? res.data : that.data.supply1.concat(res.data),//合并数据
          page_1: page + 1
        })
      }else{
          wx.showToast({
            title: '没有更多数据了',
            ico: 'error'
          })
      }
     
    })
  },

  my_supply2(that, page){
    let param = {
      ucode: app.globalData.userinfo.ucode
    }
    api.my_supply2(param, page).then(function(res){
      if (res.data.length>0){
        that.setData({
          supply2: page == 1 ? res.data : that.data.supply2.concat(res.data),//合并数据
          page_2: page + 1
        })
      }else{
          wx.showToast({
            title: '没有更多数据了',
            ico: 'error'
          })
      }
     
    })
  },

  my_supply3(that, page){
    let param = {
      ucode: app.globalData.userinfo.ucode
    }
    api.memberSupply1_index(param, page).then(function(res){
      if (res.data.length>0){
        console.log(res.data)
        that.setData({
          supply3: page == 1 ? res.data : that.data.supply3.concat(res.data),//合并数据
          page_3: page + 1
        })
      }else{
          wx.showToast({
            title: '没有更多数据了',
            ico: 'error'
          })
      }
     
    })
  },

  onPullDownRefresh:function(){
    this.onRefresh();
  },
  onRefresh:function(){
      let that = this
      //导航条加载动画
      wx.showLoading();
      wx.showNavigationBarLoading();
      setTimeout(function () {
        wx.hideNavigationBarLoading();
        //停止下拉刷新
        that.setData({
          page: 1
        })
        that.my_supply1(that, 1)
        that.my_supply2(that, 1)
        that.my_supply3(that, 1)
        wx.stopPullDownRefresh();
        wx.hideLoading();
      }, 2000);
    },

    onReachBottom:function(){
     wx.hideLoading()
      // 加载更多，滑动到底部执行此方法
     
      let that = this
      if(this.data.tabsCurrent == 0){
        let page = this.data.page_1 ;
        console.log(page)
        this.my_supply1(that, page)
      }
      if(this.data.tabsCurrent == 1){
        let page = this.data.page_2 ;
        console.log(page)
        this.my_supply2(that, page)
      }
      if(this.data.tabsCurrent == 2){
        let page = this.data.page_3 ;
        console.log(page)
        this.my_supply3(that, page)
      }
      
  },
  onStickyScroll: function(){

  },

  del_supply1: function(){
      console.log(this.data.result)
      let param = {
        ids: this.data.result
      }
      let that = this
      api.del_supply1(param).then(function(res){
        that.setData({
          result: []
         })
         that.onShow()
      })
  },

  del_supply2: function(){
    let param = {
      ids: this.data.result1
    }
    let that = this
    api.del_supply2(param).then(function(res){
      that.setData({
        result1: []
       })
       that.onShow()
    })
},
del_supply3: function(){
  let param = {
    ids: this.data.result2
  }
  let that = this
  api.memberSupply1_del(param).then(function(res){
    that.setData({
      result2: []
     })
     that.onShow()
  })
},
read: function(){
  let param = {
    ids: this.data.result2
  }
  let that = this
  api.memberSupply1_allread(param).then(function(res){
    that.setData({
      result2: []
     })
     wx.redirectTo({
       url: '/pages/message/record2/record2?tabsCurrent=2',
     })
  })
},

memberSupply1_count: function(that){
  
  let param = {
    ucode: app.globalData.userinfo.ucode
  }
  api.memberSupply1_count(param).then(function(res){
      if(res.data == '1'){
        that.setData({
          is_count: true
        })
      }else{
        that.setData({
          is_count: false
        })
      }
      console.log(res.data)
  })
}
  
})